/**
 * 
 */
package com.mc.socket;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @author w.weizhen
 *
 */
public class SocketMethod {

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		client();
	}
	
	
	private static void client()
	{
		try {
	        // 1.建立Socket，指定IP和端口
	        Socket socket = new Socket("223.68.185.218", 8368);
	        // 2.获取输出流
	        OutputStream outputStream = socket.getOutputStream();
	        // 3.向服务端发送消息
	        String message = "QN=20221659346641837;ST=31;CN=2022;PW=123456;MN=010000A8900016F000169DC0;Flag=5;CP=&&DataTime=20110221093000;a00000-Rtd=36.8,a00000-Flag=N;a21026-Rtd=20.00,w21026-Flag=N;";
	        outputStream.write(message.getBytes("UTF-8"));
	        //通过shutdownOutput高速服务器已经发送完数据,后续只能接受数据
	        socket.shutdownOutput();
	        // 4.获取输入流，接收服务端的消息
	        InputStream inputStream = socket.getInputStream();
	        byte[] bytes = new byte[1024];
	        int len;
	        StringBuilder sb = new StringBuilder();
	        while ((len = inputStream.read(bytes)) != -1) {
	        	//注意指定编码格式,发送方和接收方一定要统一,建议使用UTF-8
	            sb.append(new String(bytes, 0, len, "UTF-8"));
	        }
	        System.out.println("get message from server: " + sb);
	        // 5.关闭流，Socket
	        inputStream.close();
	        outputStream.close();
	        socket.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	private static void server()
	{
		
		
		
		
		
		
	}
	
}